package com.fqcar.fqdf.util;

import android.content.Context;
import android.os.Environment;


import com.fqdf.framework.android.util.LogUtil;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import static android.os.Environment.MEDIA_MOUNTED;

public class StorageUtil {

    private static final String INDIVIDUAL_DIR_NAME = "uil-images";

    private StorageUtil() {
    }

    /**
     * 返回应用程序缓存目录
     *
     * @param context
     * @return
     */
    public static File getCacheDirectory(Context context) {
        return getCacheDirectory(context, true);
    }

    /**
     * 返回应用程序缓存目录
     *
     * @param context
     * @param preferExternal
     * @return
     */
    public static File getCacheDirectory(Context context, boolean preferExternal) {
        File appCacheDir = null;
        String externalStorageState;
        try {
            externalStorageState = Environment.getExternalStorageState();
        } catch (NullPointerException e) { // (sh)it happens (Issue #660)
            externalStorageState = "";
        }
        if (preferExternal && MEDIA_MOUNTED.equals(externalStorageState)) {
            appCacheDir = getExternalCacheDir(context);
        }
        if (appCacheDir == null) {
            appCacheDir = context.getCacheDir();
        }
        if (appCacheDir == null) {
            String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
            LogUtil.w(StorageUtil.class, "Can't define system cache directory! ' " + cacheDirPath + " ' will be used.");
            appCacheDir = new File(cacheDirPath);
        }
        return appCacheDir;
    }

    /**
     * 获取内置缓存目录
     *
     * @param context
     * @return
     */
    public static File getIndividualCacheDirectory(Context context) {
        File cacheDir = getCacheDirectory(context);
        File individualCacheDir = new File(cacheDir, INDIVIDUAL_DIR_NAME);
        if (!individualCacheDir.exists()) {
            if (!individualCacheDir.mkdir()) {
                individualCacheDir = cacheDir;
            }
        }
        return individualCacheDir;
    }

    /**
     * 获取自己的缓存路径
     * 如果外置SD卡可用，则在SD卡根目录创建该文件夹
     * 如果外置SD卡不可用，则返回系统缓存目录
     *
     * @param context
     * @param cacheDir 缓存目录名
     * @return
     */
    public static File getOwnCacheDirectory(Context context, String cacheDir) {
        File appCacheDir = null;
        if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir);
        }
        if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) {
            appCacheDir = context.getCacheDir();
        }
        return appCacheDir;
    }

    /**
     * 获取外置SD卡缓存目录，改目录位于/SDCard/Android/data/cache
     *
     * @param context
     * @return
     */
    private static File getExternalCacheDir(Context context) {
        File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
        File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
        if (!appCacheDir.exists()) {
            if (!appCacheDir.mkdirs()) {
                LogUtil.w(StorageUtil.class, "Unable to create external cache directory");
                return null;
            }
            try {
                new File(appCacheDir, ".nomedia").createNewFile();
            } catch (IOException e) {
                LogUtil.i(StorageUtil.class, "Can't create \".nomedia\" file in application external cache directory");
            }
        }
        return appCacheDir;
    }

    public static File getOwnDataDir(Context context) {
        File appCacheDir = null;
        if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            appCacheDir = new File(Environment.getExternalStorageDirectory(), context.getPackageName());
        }
        if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) {
            appCacheDir = context.getCacheDir();
        }
        return appCacheDir;
    }

    /**
     * 获取拍照图片缓存路径
     *
     * @param context
     * @return
     */
    public static File getTakePhotoDir(Context context) {
        File chache = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        if (!chache.exists()) {
            chache.mkdirs();
        }
        return chache;
    }

    /**
     * 生成文件名
     *
     * @return
     */
    public static String generateFileName() {

        return UUID.randomUUID().toString() + ".jpg";
    }
}